

class FavoriteController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ favoriteInstanceList: Favorite.list( params ), favoriteInstanceTotal: Favorite.count() ]
    }

    def show = {
        def favoriteInstance = Favorite.get( params.id )

        if(!favoriteInstance) {
            flash.message = "Favorite not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ favoriteInstance : favoriteInstance ] }
    }

    def delete = {
        def favoriteInstance = Favorite.get( params.id )
        if(favoriteInstance) {
            try {
                favoriteInstance.delete()
                flash.message = "Favorite ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Favorite ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Favorite not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def favoriteInstance = Favorite.get( params.id )

        if(!favoriteInstance) {
            flash.message = "Favorite not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ favoriteInstance : favoriteInstance ]
        }
    }

    def update = {
        def favoriteInstance = Favorite.get( params.id )
        if(favoriteInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(favoriteInstance.version > version) {
                    
                    favoriteInstance.errors.rejectValue("version", "favorite.optimistic.locking.failure", "Another user has updated this Favorite while you were editing.")
                    render(view:'edit',model:[favoriteInstance:favoriteInstance])
                    return
                }
            }
            favoriteInstance.properties = params
            if(!favoriteInstance.hasErrors() && favoriteInstance.save()) {
                flash.message = "Favorite ${params.id} updated"
                redirect(action:show,id:favoriteInstance.id)
            }
            else {
                render(view:'edit',model:[favoriteInstance:favoriteInstance])
            }
        }
        else {
            flash.message = "Favorite not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def favoriteInstance = new Favorite()
        favoriteInstance.properties = params
        return ['favoriteInstance':favoriteInstance]
    }

    def save = {
        def favoriteInstance = new Favorite(params)
        if(!favoriteInstance.hasErrors() && favoriteInstance.save()) {
            flash.message = "Favorite ${favoriteInstance.id} created"
            redirect(action:show,id:favoriteInstance.id)
        }
        else {
            render(view:'create',model:[favoriteInstance:favoriteInstance])
        }
    }
}
